package com.maserb.monitor.web.author_manage;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.Function;
import com.maserb.monitor.entity.Role;
import com.maserb.monitor.entity.RoleFunction;
import com.maserb.monitor.service.author_manage.FunctionService;
import com.maserb.monitor.service.author_manage.RoleService;
import com.maserb.monitor.service.base.IToolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/author_manage/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    FunctionService functionService;
    @Autowired
    IToolService toolService;

    @RequestMapping(value = {"/", "/index"})
    public String getIndex() {
        return "author_manage/role/index";
    }

    @RequestMapping("/getAddRole")
    public ModelAndView getAddUser() {
        ModelAndView result = new ModelAndView();
        result.setViewName("author_manage/role/add");
        List<Function> funcList = functionService.findAllMenu();
        result.addObject("funcList", funcList);
        return result;
    }

    @RequestMapping("/getUpdateRole/{id}")
    public ModelAndView getUpdateRole(@PathVariable String id) {
        ModelAndView result = new ModelAndView();
        result.setViewName("author_manage/role/edit");
        List<Function> funcList = functionService.findAllMenu();
        result.addObject("funcList", funcList);

        if (!StringUtils.isEmpty(id)) {
            Role role = roleService.findOne(id);
            result.addObject("role", role);

            List<String> existsFunctionIds = new ArrayList<String>();
            List<RoleFunction> existsFunction = roleService.findFunctionByRole(role);
            for (RoleFunction roleFun : existsFunction) {
                existsFunctionIds.add(roleFun.getTSFunction().getId());
            }
            result.addObject("existsFunctionIds", existsFunctionIds);
        }
        return result;
    }

    @RequestMapping("/doAddRole")
    @ResponseBody
    public AjaxResult<Boolean> doAddRole(final HttpServletRequest request, final Role role, final String[] functionId) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                toolService.initCreateInfo(role);
                roleService.saveRole(role, functionId);
                toolService.init();
                return true;
            }
        });
    }

    @RequestMapping("/doUpdateRole")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateRole(final HttpServletRequest request, final Role role, final String[] functionId) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                Role dbRole = roleService.findOne(role.getId());
                dbRole.setRoleCode(role.getRoleCode());
                dbRole.setRoleName(role.getRoleName());
                toolService.initUpdateInfo(dbRole);
                roleService.saveRole(role, functionId);
                toolService.init();
                return true;
            }
        });
    }

    @RequestMapping("/doListRole")
    @ResponseBody
    public PageResponseResult<Role> doListRole(PageRequestParam page, final String name) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<Role>>() {
            @Override
            public Page<Role> invoke(Pageable var1) throws Exception {
                Page<Role> page = roleService.findAll(var1, name);
                return page;
            }
        });
    }

    @RequestMapping("/doDeleteRole")
    @ResponseBody
    public AjaxResult<Boolean> doDeleteRole(@RequestParam(value = "keys[]") final ArrayList<String> keys) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                roleService.deleteRoleByKeys(keys);
                return true;
            }
        });
    }
}